web/src/pages/[category].astro

164 lines · 3482 bytes

  1---
  2import Layout from '@layouts/Layout.astro';
  3import SectionList from '@components/things/SectionList.astro';
  4import Main from '@components/scafold/MainCard.astro';
  5
  6import { fetchData, slugify } from '@utils/fetch-data';
  7
  8import type { Section } from '../types/Service';
  9import Button from '@components/form/Button.astro';
 10
 11interface Props {
 12	title: string;
 13	sections: Section[];
 14}
 15
 16const { title, sections } = Astro.props;
 17
 18const categories = (await fetchData()).categories;
 19
 20export async function getStaticPaths() {
 21	const pages = await fetchData().then((data) => {
 22		return data.categories.map((category) => {
 23			return {
 24				category: slugify(category.name),
 25				title: category.name,
 26				sections: category.sections,
 27			};
 28		});
 29	});
 30
 31	return pages.map(({ category, title, sections }) => {
 32		return {
 33			params: { category },
 34			props: { title, sections },
 35		};
 36	});
 37}
 38
 39const makeCarasolData = () => {
 40	return {
 41		'@context': 'https://schema.org',
 42		'@type': 'ItemList',
 43		itemListElement: [
 44			sections.map((section, index) => ({
 45				'@type': 'ListItem',
 46				position: index + 1,
 47				url: `https://awesomeprivacy.com/${slugify(title)}/${slugify(section.name)}`,
 48				item: {
 49					'@type': 'Service',
 50					name: section.name,
 51					url: `https://awesomeprivacy.com/${slugify(title)}/${slugify(section.name)}`,
 52				},
 53			})),
 54		],
 55	};
 56};
 57
 58const makeBreadcrumbs = () => {
 59	return [
 60		{ name: 'Home', item: '/' },
 61		{ name: title, item: slugify(title) },
 62	];
 63};
 64
 65const makePaginationLinks = () => {
 66	const index = categories.findIndex((category) => category.name === title);
 67	const previousCategory = index > 0 ? categories[index - 1].name : null;
 68	const nextCategory =
 69		index < categories.length - 1 ? categories[index + 1].name : null;
 70	return { previous: previousCategory, next: nextCategory };
 71};
 72
 73const { previous, next } = makePaginationLinks();
 74---
 75
 76<Layout
 77	title={`${title} | Awesome Privacy`}
 78	breadcrumbs={makeBreadcrumbs()}
 79	customSchemaJson={makeCarasolData()}
 80>
 81	<Main>
 82		<div class="breadcrumbs">
 83			<span>
 84				<a href="/">Awesome Privacy</a>
 85				➔ <a href={`/${slugify(title)}`}>{title}</a>
 86			</span>
 87		</div>
 88		<SectionList title={title} sections={sections} bigTitle={true} />
 89		Or, <a href={`/all#${slugify(title)}`}>Browse All {title}</a>
 90		<div class="pagination-navigation">
 91			{
 92				previous ? (
 93					<Button url={`/${slugify(previous)}`}>
 94						<span>← Previous</span>
 95						<p>{previous}</p>
 96					</Button>
 97				) : (
 98					<p class="nothing" />
 99				)
100			}
101			{
102				next && (
103					<Button url={`/${slugify(next)}`}>
104						<span>Next →</span>
105						<p>{next}</p>
106					</Button>
107				)
108			}
109		</div>
110	</Main>
111</Layout>
112
113<style lang="scss">
114	.pagination-navigation {
115		display: flex;
116		justify-content: space-between;
117		align-items: center;
118		padding: 1rem 0;
119		:global(.button) {
120			min-width: 120px;
121			width: fit-content;
122			padding: 0.25rem 1rem;
123			text-align: right;
124			&:first-child {
125				text-align: left;
126			}
127			p {
128				margin: 0;
129				font-weight: normal;
130				font-size: 1rem;
131			}
132			span {
133				font-size: 0.8rem;
134			}
135		}
136		.nothing {
137			width: 120px;
138		}
139		.go-to-category {
140			color: var(--foreground);
141			font-size: 0.8rem;
142			opacity: 0.5;
143		}
144	}
145
146	.breadcrumbs {
147		opacity: 0.5;
148		font-size: 0.8rem;
149		position: absolute;
150		right: 1rem;
151		top: 1rem;
152		a {
153			color: var(--foreground);
154			transition: all 0.15s ease-in-out;
155
156			&:hover {
157				color: var(--accent);
158			}
159		}
160		@media (max-width: 768px) {
161			display: none;
162		}
163	}
164</style>